import { addMalware, findAll, findById } from '../domain/malware';
import {
  stixDomainObjectAddRelation,
  stixDomainObjectCleanContext,
  stixDomainObjectDelete,
  stixDomainObjectDeleteRelation,
  stixDomainObjectEditContext,
  stixDomainObjectEditField,
} from '../domain/stixDomainObject';
import { loadThroughDenormalized } from './stix';
import { INPUT_KILLCHAIN } from '../schema/general';
import { INPUT_SAMPLE } from '../schema/stixRefRelationship';

const malwareResolvers = {
  Query: {
    malware: (_, { id }, context) => findById(context, context.user, id),
    malwares: (_, args, context) => findAll(context, context.user, args),
  },
  Malware: {
    killChainPhases: (malware, _, context) => loadThroughDenormalized(context, context.user, malware, INPUT_KILLCHAIN, { sortBy: 'phase_name' }),
    samples: (malware, _, context) => loadThroughDenormalized(context, context.user, malware, INPUT_SAMPLE, { sortBy: 'name' }),
  },
  Mutation: {
    malwareEdit: (_, { id }, context) => ({
      delete: () => stixDomainObjectDelete(context, context.user, id),
      fieldPatch: ({ input, commitMessage, references }) => stixDomainObjectEditField(context, context.user, id, input, { commitMessage, references }),
      contextPatch: ({ input }) => stixDomainObjectEditContext(context, context.user, id, input),
      contextClean: () => stixDomainObjectCleanContext(context, context.user, id),
      relationAdd: ({ input }) => stixDomainObjectAddRelation(context, context.user, id, input),
      relationDelete: ({ toId, relationship_type: relationshipType }) => stixDomainObjectDeleteRelation(context, context.user, id, toId, relationshipType),
    }),
    malwareAdd: (_, { input }, context) => addMalware(context, context.user, input),
  },
};

export default malwareResolvers;
